package Employee

import (
	"context"

	v1 "gitee.com/liangziyisheng/caas-platform-mc/api/employee/v1"
	"gitee.com/liangziyisheng/caas-platform-mc/internal/consts"
	"gitee.com/liangziyisheng/caas-platform-mc/internal/dao"
	"gitee.com/liangziyisheng/caas-platform-mc/internal/service"

	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/glog"
)

type sEmployee struct{}

var l = g.Log("logger for Employee")

func New() *sEmployee {
	return &sEmployee{}
}

func init() {
	l.SetFlags(glog.F_FILE_LONG)
	service.RegisterEmployee(New())
}

func (s *sEmployee) List(ctx context.Context, in *v1.ListInput) (out *v1.ListOutput, err error) {
	cls := dao.Employee.Columns()
	md := dao.Employee.Ctx(ctx).Where(cls.TenantCode, in.TenantCode)
	if in.EmployeeNo != "" {
		md = md.Where(cls.EmployeeNo, in.EmployeeNo)
	}
	if in.NationalId != "" {
		md = md.Where(cls.NationalId, in.NationalId)
	}
	if in.Email != "" {
		md = md.Where(cls.Email, in.Email)
	}
	if in.Phone != "" {
		md = md.Where(cls.Phone, in.Phone)
	}
	if in.DepartmentId != 0 {
		md = md.Where(cls.DepartmentId, in.DepartmentId)
	}
	if in.ManagerId != 0 {
		md = md.Where(cls.ManagerId, in.ManagerId)
	}
	out = &v1.ListOutput{}
	err = md.Limit((in.Page-1)*in.Size, in.Size).ScanAndCount(&out.Data, &out.Total, true)
	if err != nil {
		l.Error(ctx, err)
		return nil, gerror.NewCode(gcode.WithCode(consts.ListEmployeeFailed, in))
	}
	return out, nil
}

func (s *sEmployee) Add(ctx context.Context, in *v1.AddInput) (out *v1.AddOutput, err error) {
	l.Info(ctx, "Employee add")
	out = &v1.AddOutput{}
	out.Id, err = dao.Employee.Ctx(ctx).InsertAndGetId(in.Employee)
	if err != nil {
		l.Error(ctx, err)
		return nil, gerror.NewCode(gcode.WithCode(consts.AddFailed, in.Employee))
	}
	return out, nil
}

func (s *sEmployee) Delete(ctx context.Context, in *v1.DeleteInput) (out *v1.DeleteOutput, err error) {
	cls := dao.Employee.Columns()
	rs, err := dao.Employee.Ctx(ctx).Where(cls.Id, in.Id).Where(cls.TenantCode, in.TenantCode).Delete()
	if err != nil {
		l.Error(ctx, err)
		return nil, gerror.NewCode(gcode.WithCode(consts.DeleteFailed, in.Id))
	}
	out = &v1.DeleteOutput{}
	if out.Rows, err = rs.RowsAffected(); err != nil {
		l.Error(ctx, err)
		return nil, gerror.NewCode(gcode.WithCode(consts.DeleteFailed, in.Id))
	}
	if out.Rows == 0 {
		return nil, gerror.NewCode(gcode.WithCode(consts.DeleteFailed, in.Id))
	}
	return out, nil
}

func (s *sEmployee) Update(ctx context.Context, in *v1.UpdateInput) (out *v1.UpdateOutput, err error) {
	out = &v1.UpdateOutput{}
	cls := dao.Employee.Columns()
	out.Rows, err = dao.Employee.Ctx(ctx).Data(in.Employee).Where(cls.Id, in.Id).Where(cls.TenantCode, in.TenantCode).UpdateAndGetAffected()
	if err != nil {
		l.Error(ctx, err)
		return nil, gerror.NewCode(gcode.WithCode(consts.UpdateFailed, in.Employee))
	}
	if out.Rows == 0 {
		return nil, gerror.NewCode(gcode.WithCode(consts.UpdateFailed, in.Id))
	}
	return out, nil
}
